import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/Login'
  },
  {
    path: '/Login',
    name: 'Login',
    component: () => import('@/views/login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/home.vue'),
    children: [
      {
        path: '/home',
        redirect: '/welcome'
      },
      {
        path: '/welcome',
        component: () => import('@/views/Welcome.vue')
      }, {
        path: '/users',
        component: () => import('@/components/user.vue')
      }
    ]
  }
]
const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/Login') {
    return next()
  } else {
    const token = window.sessionStorage.getItem('token')
    if (!token) {
      return next('/Login')
    }
  }
  next()
})
export default router
